-
Notifications
You must be signed in to change notification settings - Fork 432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve doc on implementing Default
and Clone
#383
Conversation
Looks good to me! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me as well!
b6e50ee
to
aa3b147
Compare
rand_core/src/lib.rs
Outdated
/// support optional at the crate level in PRNG libs. | ||
/// - `Clone`, but only if the clone will have identical output to the original | ||
/// (i.e. be a true clone), or if the clone would be another handle to the | ||
/// same generator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I never really got the point of saying this. If you put anything in an Rc<..>
type then clone()
will copy the reference, not the object — so why even say it?
If you're going to be pedantic like this then you might as well update the next point about Copy
too, though I don't see much point.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied it from your comment, but now understand why you left it out. With OsRng
in mind I think it starts to make more sense, as that is the same principle but without Rc<..>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort of. I'm wondering if we should say much about Clone
at all now. Maybe just that on normal PRNGs clone
should imply identical output? Even that may not make sense given that we might try to make StdRng
reseed on clone in the future(?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just reworded it as "Clone
if possible"
aa3b147
to
34ebc80
Compare
Spelling out my concerns on implementing
I think you mean |
Yes, I meant I don't see any reason |
d91e110
to
f6d6fe4
Compare
Added a commit to implement |
Default
Default
and Clone
Does it make sense to implement |
I don't care much about the |
src/jitter.rs
Outdated
@@ -51,6 +51,7 @@ const MEMORY_SIZE: usize = MEMORY_BLOCKS * MEMORY_BLOCKSIZE; | |||
/// [Jitterentropy](http://www.chronox.de/jent.html) version 2.1.0. | |||
/// | |||
/// [`OsRng`]: ../os/struct.OsRng.html | |||
#[derive(Clone)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we at the very least need to clear data_half_used
to prevent next_u32
returning the same result from each part?
Otherwise we could simply implement this with JitterRng::new
— except that timer
might not be the default function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Glad you are sharp 😄.
inner: self.inner.clone(), | ||
reseeder: self.reseeder.clone(), | ||
threshold: self.threshold, | ||
bytes_until_reseed: 0, // reseed clone on first use |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice way of forcing a reseed!
6394566
to
a68ec1d
Compare
data: self.data, | ||
rounds: self.rounds, | ||
timer: self.timer, | ||
mem_prev_index: self.mem_prev_index, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think mem_prev_index
should be adjusted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we are okay leaving it as is. They probably have different locations for the memory slice on the stack. Having the same location twice should not make the CPU much better at predicting. And after the first round the values will diverge, as memaccess
is called a variable number of times.
Okay, I was going to merge, but there are conflicts. |
a68ec1d
to
6060f1a
Compare
Rebased |
Fixes #378.
cc @newpavlov